﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; 
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.CommandWpf;
using System.Threading;

namespace WpfApp1
{
    public class Win2VM : ViewModelBase
    {
        System.Timers.Timer timer = new System.Timers.Timer(1000);
        Window2 View;
        public Win2VM(Window2 view)
        {
            this.View = view  ;
            List<T> TS = new List<T>();
            TT = new ObservableCollection<T>();
            TT.Add(new T { ID = 1, Name = "AS1" });
            TT.Add(new T { ID = 2, Name = "AS2" });
            TT.Add(new T { ID = 3, Name = "AS3" });
            TT.Add(new T { ID = 4, Name = "AS4" });
            TT.Add(new T { ID = 5, Name = "AS5" });
            TT.Add(new T { ID = 6, Name = "AS6" });
            TT.Add(new T { ID = 7, Name = "AS7" });
            TT.Add(new T { ID = 8, Name = "AS8" });
            TT.Add(new T { ID = 9, Name = "AS9" });
            TT.Add(new T { ID = 10, Name = "AS10" });
            TT.Add(new T { ID = 11, Name = "AS10" });
            TT.Add(new T { ID = 12, Name = "AS10" });
            TT.Add(new T { ID = 13, Name = "AS10" });
            TT.Add(new T { ID = 14, Name = "AS10" });
            TT.Add(new T { ID = 15, Name = "AS10" });
            TT.Add(new T { ID = 16, Name = "AS10" });
            TT.Add(new T { ID = 17, Name = "AS10" });
            TT.Add(new T { ID = 18, Name = "AS10" });
            TT.Add(new T { ID = 19, Name = "AS10" });
            TT.Add(new T { ID = 20, Name = "AS20" });
            TT.Add(new T { ID = 21, Name = "AS21" });
            TT.Add(new T { ID = 22, Name = "AS22" });
            TT.Add(new T { ID = 23, Name = "AS23" });
            TT.Add(new T { ID = 24, Name = "AS24" });
            // TT = new ObservableCollection<T>(TS);
            timer.Elapsed += Timer_Elapsed;
            //timer.Enabled = true;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.View.Dispatcher.BeginInvoke(new Action(() => {

                TT[0].Name = DateTime.Now.ToString();
                TT.Add(new T { ID = 2, Name = "AS2" });
                Console.WriteLine("aa");
            }));
            
        }

        ObservableCollection<T> tt;
        public ObservableCollection<T> TT
        {
            get { return tt; }
            set
            {
                tt = value;
                RaisePropertyChanged("TT");
            }
        }
        RelayCommand cmdClick;
        public RelayCommand CmdClick
        {
            get { return cmdClick ?? (cmdClick = new RelayCommand(() => DoCmdClick())); }
        }
        void DoCmdClick()
        {
            Task.Run(() =>
            {
                if (tt != null && tt.Count > 0)
                {
                    foreach (var ttt in tt)
                    {
                        //Task.Run(() => ReadWeight(ttt));
                        var task = new Task(() => ReadWeight(ttt));
                        tasks.Add(task);
                        
                    }
                    foreach (var t in tasks)
                    {
                        t.Start();
                    }
                }
            });
        }
        List<Task> tasks = new List<Task>();
        void ReadWeight(T t)
        {
            while (true)
            {
                t.Name = string.Format("{0} {1}", t.ID, DateTime.Now.Ticks);
                Console.WriteLine("{0} {1}", Thread.CurrentThread.ManagedThreadId, t.ToString());
               // Thread.Sleep(50);
            }
        } 
    }

    public class T:ViewModelBase
    {
        int id;
        public int ID
        {
            get { return id; }
            set
            {
                id = value;
                RaisePropertyChanged("ID");
            }
        }

        string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }
        public override string ToString()
        {
            return string.Format("ID:{0},Name:{1}", ID, Name);
        }
    }
}
